package com.ydd.moudle.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.MainThread;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ydd.R;
import com.ydd.base.BaseFragment;
import com.ydd.model.event.MinaEvent;
import com.ydd.model.mina.MinaModel;
import com.ydd.moudle.me.LoginActivity;
import com.ydd.moudle.me.SettingActivity;
import com.ydd.moudle.message.MessageActivity;
import com.ydd.network.http.RequestCenter;
import com.ydd.network.mina.SessionManager;
import com.ydd.util.db.SPManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;

/**
 * Created by tao on 17-3-11.
 */

public class MessageFragment extends BaseFragment implements View.OnClickListener {
    /**
     * UI
     */
    private View mContentView;
    private RelativeLayout mMessageLayout;
    private RelativeLayout mZanLayout;
    private RelativeLayout mImoocLayout;
    private TextView mTipView;
    private TextView mTipZanView;
    private TextView mTipMsgView;

    /**
     * Data
     */
    private MinaModel mData;
    private String data;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_message_layout, container, false);
        EventBus.getDefault().register(this);
        initHeaderView(getString(R.string.message), mContentView);

        initView();
        return mContentView;
    }

    private void initView() {
        mMessageLayout = (RelativeLayout) mContentView.findViewById(R.id.message_layout);
        mImoocLayout = (RelativeLayout) mContentView.findViewById(R.id.imooc_layout);
        mZanLayout = (RelativeLayout) mContentView.findViewById(R.id.zan_layout);
        mTipView = (TextView) mContentView.findViewById(R.id.tip_view);
        mTipZanView = (TextView) mContentView.findViewById(R.id.zan_tip_view);
        mTipMsgView = (TextView) mContentView.findViewById(R.id.unread_tip_view);

        mImoocLayout.setOnClickListener(this);
        mZanLayout.setOnClickListener(this);
        mMessageLayout.setOnClickListener(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMinaEvent(MinaEvent event) {
//        Gson gson = new Gson();
//        mData = gson.fromJson((String) event.getMessage(), MinaModel.class);
        data = event.getMessage().toString();
        mTipView.setVisibility(View.VISIBLE);
        mTipView.setText(data);
        //要改成三个界面都更新
//        if (mData != null) {
//            if (mData.getData().getMsgValues() != null && mData.getData().getMsgValues().size() > 0) {
//                mTipMsgView.setVisibility(View.VISIBLE);
//                mTipMsgView.setText(String.valueOf(mData.getData().getMsgValues().size()));
//            } else {
//                mTipMsgView.setVisibility(View.GONE);
//            }
//
//            if (mData.getData().getZanValues() != null && mData.getData().getZanValues().size() > 0) {
//                mTipZanView.setVisibility(View.VISIBLE);
//                mTipZanView.setText(String.valueOf(mData.getData().getZanValues().size()));
//            } else {
//                mTipZanView.setVisibility(View.GONE);
//            }
//
//            if (mData.getData().getSystemValues() != null && mData.getData().getSystemValues().size() > 0) {
//                mTipView.setVisibility(View.VISIBLE);
//                mTipView.setText(String.valueOf(mData.getData().getSystemValues().size()));
//            } else {
//                mTipView.setVisibility(View.GONE);
//            }
//        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.zan_layout:
//                gotoMessageActivity(2);
//                break;
//            case R.id.message_layout:
//                gotoMessageActivity(1);
//                break;
            case R.id.imooc_layout:
                gotoMessageActivity(3);
                break;

            default:
                break;
        }
    }

    private void gotoMessageActivity(int type) {
        boolean isLogin = SPManager.getInstance().getBoolean(SPManager.IS_LOGIN_USER, false);
            if (isLogin) {
                Intent intent = new Intent(getActivity(), MessageActivity.class);
//                if (mData != null) {
//                    switch (type) {
//                        case 1:
//                            intent.putExtra(MessageActivity.DATA_LIST, (Serializable) mData.getData().getMsgValues());
//                            mTipMsgView.setVisibility(View.GONE);
//                            break;
//                        case 2:
//                            intent.putExtra(MessageActivity.DATA_LIST, (Serializable) mData.getData().getZanValues());
//                            mTipZanView.setVisibility(View.GONE);
//                            break;
//                        case 3:
//                            intent.putExtra(MessageActivity.DATA_LIST, (Serializable) mData.getData().getSystemValues());
//                            mTipView.setVisibility(View.GONE);
//                            break;
//                    }
//                } else {
//
//                }
                intent.putExtra(MessageActivity.TYPE, type);
                intent.putExtra(MessageActivity.DATA_LIST,data);
                mTipView.setVisibility(View.GONE);
                startActivity(intent);
            } else {
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }

    }
}
